''
''
'' README SECTION
''
''
''
''
''
''
''
''
''
''
''
{------------- REVISION HISTORY -------------
v1.0 - 04/02/2012, first draft
}

CON

'-----------------------------------------------------------------
  'CLK Config

  _clkmode                      = xtal1 + pll16x
  _xinfreq                      = 5_000_000

'-----------------------------------------------------------------
  'Pin assignements

  'SD CARD '------------------------------------------------------
  SD_DO                         = 0
  SD_CLK                        = 1
  SD_DIN                        = 2
  SD_CS                         = 3

  'Primary Connection - Serial - XBEE '----------------------------
  UART_RX                       = 4
  UART_TX                       = 5

  'Interconnectivity Connection (Arduino <-> Propeller) '----------
  ARDUINO_RX                    = 6
  ARDUINO_TX                    = 7

  'GPS UART Lines '------------------------------------------------
  GPS_RX                        = 8
  GPS_TX                        = 9

  'LCD UART Lines '------------------------------------------------
  LCD_RX                        = 10
  LCD_TX                        = 11

  'EEPROM I2C Lines '----------------------------------------------
  I2C_SDA                       = 11
  I2C_SCL                       = 12

  '7 Segment Light Status '----------------------------------------
  PIN_1                         = 13
  PIN_2                         = 14
  PIN_3                         = 15 'gnd8
  PIN_4                         = 16
  PIN_5                         = 17
  PIN_6                         = 18
  PIN_7                         = 19
  PIN_8                         = 20 'gnd
  PIN_9                         = 21
  PIN_10                        = 22

  'Pulse Oximeter '------------------------------------------------
  PULSE_OX_Freq                 = 23
  IR_LED                        = 34
  RED_LED                       = 25

  'Temperature Readout '-------------------------------------------
  TEMP_CLK                      = 26
  TEMP_CS                       = 27
  TEMP_DOUT                     = 28

  'Unused Pins '---------------------------------------------------
  UNUSED_1                      = 29
  UNUSED_2                      = 30
  UNUSED_3                      = 31
  UNUSED_4                      = 32

  'BAUD RATES '----------------------------------------------------
  UART_BAUD                     = 115200
  ARDUINO_BAUD                  = 115200
  GPS_BAUD                      = 115200
  LCD_BAUD                      = 115200

  'Serial Terminal Commands '--------------------------------------
  cmd_length                    = 256
  num_tokens                    = 16 ' change

OBJ
  I2C                           :"I2C_Driver"
  GPS                           :"GPS_NMEA"
  COMS_LCD                      :"FullDuplexSerial"
  COMS_GPS                      :"FullDuplexSerial"
  COMS_ARDUINO                  :"FullDuplexSerial"
  COMS_MAIN                     :"FullDuplexSerial"
  SD                            :"fsrw"
  PULSE_OX                      :"tsl230"
  TEMP                          :"MAX6675_K_ThermoCouple"

VAR
  long tokens[num_tokens]
  long tindex
  byte tin[cmd_length]

  byte moduleStatus
  byte testComLCD
  byte testComMain

  byte hexidecimalCharacters[9]

  long GPSBuffer
  long ArduinoBuffer

PUB Main | input, action_enable

'-----------------------------------------------------------------
'Init
  'Start up the Serial Objects - Terminal and LCD
    'Init LCD
    'Init Serial
    moduleStatus |= 0000_0000_0001 'Coms Init status

    COMS_LCD.start(LCD_RX, LCD_TX, 0, LCD_BAUD)
    COMS_MAIN.start(UART_RX, UART_TX, 0, UART_BAUD)

    COMS_LCD.rxflush 'flush both pipes
    COMS_MAIN.rxflush

    testComLCD := COMS_LCD.rxcheck 'get -1 (empty)
    testComMain := COMS_MAIN.rxcheck

    if((NOT(testComLCD & testComMain)) == 0) 'check pipes
      COMS_LCD.str(string(13, "LCD Booted", 13, "MAIN Coms Booted", 13))
      COMS_MAIN.str(string("@LCD up and running", 13, "@MAIN Coms up and running", 13))

      moduleStatus |= 0000_0000_0001 ' cahnge state

'-----------------------------------------------------------------

  'Start All Drivers
    'LEDs
      'Check LED
      moduleStatus |= 0000_0000_0010

    'SPI SD Card
      'Check SD Card
      'Print Sysinfo
      'Print the Ack Message
      'Print File Headers
      moduleStatus |= 0000_0000_0100

    'Init GPS
      'Check GPS
      moduleStatus |= 0000_0000_1000

    'Init Arduino
      'Check Comms and responsivness
      moduleStatus |= 0000_0001_0000

'-----------------------------------------------------------------
    COMS_MAIN.str(string("@Version Information:", 13)) 'display version info

    COMS_MAIN.str(string("@VitalPAQ      rev 1.0      04/03/2012", 13))
    COMS_MAIN.str(string("@-------------------------------------", 13))
    COMS_MAIN.str(string("@Main OS       rev 1.0      04/03/2012", 13))
    COMS_MAIN.str(string("@Terminal      rev 1.0      04/03/2012", 13))
    COMS_MAIN.str(string("@Comm Driver   rev 1.0      04/03/2012", 13))
    COMS_MAIN.str(string("@LCD Driver    rev 1.0      04/03/2012", 13))
    COMS_MAIN.str(string("@GPS Driver    rev 1.0      04/03/2012", 13))
    COMS_MAIN.str(string("@Arduino OS    rev 1.0      04/03/2012", 13))
    COMS_MAIN.str(string("@Sensor Driver rev 1.0      04/03/2012", 13))
    COMS_MAIN.str(string("@File System   rev 1.0      04/03/2012", 13))

'-----------------------------------------------------------------
'Query state
  'Wait for Start Command or List Commands Cmd

  tindex := 0
  bytefill(@tin, 0, cmd_length) 'fill array with 0s
  waitcnt(clkfreq/2 + cnt) 'wait a bit

  COMS_MAIN.str(string("@Entering Query State...", 13))
  COMS_MAIN.str(string("@Waiting for START CMD...", 13))

  COMS_MAIN.str(string("@VitalPAQ >", 13)) 'cmd prompt

  moduleStatus |= 0000_0010_0000 'change status

  repeat 'infinte loop till we get start
    input := COMS_MAIN.rx 'get input
    action_enable := false
    repeat while input => 0 ' we got something
      action_enable := true
      COMS_MAIN.tx(input) 'echo chars
      if input == $08 'backspace
        tindex--
        tindex #>=0
      elseif input == $0D '[Enter]
        input := -1
        byte[@tin][tindex] := 0
        tindex := 0
        if checkStart(@tin)
          COMS_MAIN.str(string("@START CMD VALID Proceeding...", 13))
          quit
      else
        byte[@tin][tindex] := input
        tindex++
        tindex <#= cmd_length-1
      input := COMS_MAIN.rxcheck 'next char

'-----------------------------------------------------------------
'terminal / start cogs

  '"start"
  'Send command to start
      'Start Sensors:
        'Init EEPROM 0 buffering
        'Temp
        'Light to frequency
        'IR and RED LED
        'Arduino

PRI checkStart(command_string_pointer) | good, ntok

  ntok := tokenize(command_string_pointer, @tokens, num_tokens)
  if ntok < 1
    return false

  if strcomp(tokens[0], string("START"))
    return true

PRI tokenize( string_ptr, token_ptr, max_tokens ) : found_tokens | slen, was_ws, is_ws, i

  ' go through the string, storing the lead pointer to any
  ' non-whitespace tokens, and zero terminating each token
  slen := strsize( string_ptr )-1
  was_ws := true

  repeat i from 0 to slen
    ' is this important?
    is_ws := byte[string_ptr][i] < $21
    if is_ws
      ' make it a 0, so tokens will be terminated
      byte[string_ptr][i] := 0
    else
      ' well, it's not white-space...can I upper-case it?
      if (byte[string_ptr][i] > $60) and (byte[string_ptr][i] < $7B)
        byte[string_ptr][i] -= $20
      ' it may be interesting
      if was_ws
        ' yep, we just switched..store this token
        long[token_ptr][found_tokens] := string_ptr + i
        found_tokens++
      if found_tokens => max_tokens
        return max_tokens
  ' and move on
  was_ws := is_ws
' done, and the return value is in found_tokens

PUB decimalToNumber(characters) | buffer, counter

  buffer := byte[characters]
  counter := (strsize(characters) <# 11)

  repeat while(counter--)
    result *= 10
    result += lookdownz(byte[characters++]: "0".."9")

  if(buffer == "-")
    -result

PUB hexidecimalToNumber(characters) : buffer | counter

  counter := (strsize(characters) <# 8)

  repeat while(counter--)
    buffer <<= 4
    buffer += lookdownz(byte[characters++]: "0".."9", "A".."F")

PUB numberToHexidecimal(number, length)

  repeat result from 7 to 0
    hexidecimalCharacters[result] := lookupz((number & $F): "0".."9", "A".."F")
    number >>= 4

    return @hexidecimalCharacters[(8 - ((length <# 8) #> 0))]

DAT
 ACK_msg      byte             "[OK]",0
 ERROR_msg    byte             "[ERROR]",0

